package aspectjlib.visual;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class Grapher extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Grapher(String[] args) throws IOException {
		int n = Integer.parseInt(args[1]);
		FileReader fr = new FileReader(args[0]);
		BufferedReader br = new BufferedReader(fr);
		String currentLine = br.readLine();
		StringTokenizer st = new StringTokenizer(currentLine, ",");
		String xLabel = st.nextToken();
		st.nextToken();
		int z = n;
		while (z > 0) {
			st.nextToken();
			z--;
		}
		String yLabel = st.nextToken();


		String xValue, threadId;
		Long yValue;
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		while ((currentLine = br.readLine()) != null) {
			st = new StringTokenizer(currentLine, ",");
			xValue = st.nextToken();
			threadId = st.nextToken();
			z = n;
			while (z > 0) {
				st.nextToken();
				z--;
			}
			yValue = Long.parseLong(st.nextToken());
			dataset.addValue(yValue, threadId, xValue);
		}
		
		br.close();
		fr.close();
		
		JFreeChart chart = createChart(dataset, xLabel, yLabel);
		ChartUtilities.saveChartAsPNG(new File("C:/Users/Michael/Level 4 Project/peerreview-arch/logs/aspect.png"), chart, 800, 600);
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setFillZoomRectangle(true);
		chartPanel.setMouseWheelEnabled(true);
		chartPanel.setPreferredSize(new Dimension(800, 600));
        setContentPane(chartPanel);
	}
	
	public static void main (String[] args) throws IOException {
		Grapher bc = new Grapher(args);
		bc.pack();
		bc.setVisible(true);
	}

	/**
	 * Creates a sample chart.
	 *
	 * @param dataset  the dataset.
	 *
	 * @return The chart.
	 */
	private static JFreeChart createChart(CategoryDataset dataset, String xLabel, String yLabel) {

		// create the chart...
		JFreeChart chart = ChartFactory.createBarChart(
				xLabel + " vs " + yLabel,	// chart title
				xLabel,						// domain axis label
				yLabel,						// range axis label
				dataset,					// data
				PlotOrientation.VERTICAL,	// orientation
				false,						// include legend
				true,						// tooltips?
				false						// URLs?
				);

		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

		// set the background color for the chart...
		chart.setBackgroundPaint(Color.white);

		// get a reference to the plot for further customisation...
		CategoryPlot plot = (CategoryPlot) chart.getPlot();

		// ******************************************************************
		//  More than 150 demo applications are included with the JFreeChart
		//  Developer Guide...for more information, see:
		//
		//  >   http://www.object-refinery.com/jfreechart/guide.html
		//
		// ******************************************************************

		// set the range axis to display integers only...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		// disable bar outlines...
		BarRenderer renderer = (BarRenderer) plot.getRenderer();
		renderer.setDrawBarOutline(false);

		// set up gradient paints for series...
		GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue,
				0.0f, 0.0f, new Color(0, 0, 64));
		GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green,
				0.0f, 0.0f, new Color(0, 64, 0));
		GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red,
				0.0f, 0.0f, new Color(64, 0, 0));
		renderer.setSeriesPaint(0, gp0);
		renderer.setSeriesPaint(1, gp1);
		renderer.setSeriesPaint(2, gp2);

		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setCategoryLabelPositions(
				CategoryLabelPositions.createUpRotationLabelPositions(
						Math.PI / 6.0));
		// OPTIONAL CUSTOMISATION COMPLETED.

		return chart;

	}

}